package com.birdboot.controller;

import com.birdboot.annotations.Controller;
import com.birdboot.annotations.RequestMapping;
import com.birdboot.entity.User;
import com.birdboot.http.HttpServletRequest;
import com.birdboot.http.HttpServletResponse;
import com.birdboot.util.DBUtil;

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Controller
public class UserController {
    private static File userDir;
    static {
        userDir = new File("./users");
        if(!userDir.exists()){
            userDir.mkdirs();
        }
    }
    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request , HttpServletResponse response ){
        String username  = request.getParameters("username");
        String password  = request.getParameters("password");
        String nickname  = request.getParameters("nickname");
        String age  = request.getParameters("age");
        System.out.println(username+","+password+","+nickname+","+age);
        if(
                username==null || username.isEmpty() ||
                password==null || password.isEmpty() ||
                nickname==null || nickname.isEmpty() ||
                age==null || age.isEmpty() ||
                !age.matches("[0-9]+")
        ){
            response.sendRedirect("/reg_info_error.html");
            return;
        }
        int userAge = Integer.parseInt(age);
        try(Connection connection = DBUtil.getConnection();) {
            String sql1 = "SELECT username FROM userinfo WHERE username=?;";
            PreparedStatement ps1 = connection.prepareStatement(sql1);
            ps1.setString(1,username);
            ResultSet rs1 = ps1.executeQuery();
            if(rs1.next()){
                response.sendRedirect("/reg_have_user.html");
            }else {
                String sql2 = "INSERT INTO userinfo(username,password,nickname,age) VALUES (?,?,?,?);";
                PreparedStatement ps2 = connection.prepareStatement(sql2);
                ps2.setString(1,username);
                ps2.setString(2,password);
                ps2.setString(3,nickname);
                ps2.setInt(4,userAge);
                int num = ps2.executeUpdate();
                if(num>0){
                    response.sendRedirect("/reg_success.html");
                }{
                    System.out.println("注册失败");
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
//        User user = new User(username ,password ,nickname ,userAge);
//        File file = new File(userDir,username+".obj");
//        if(file.exists()){
//            response.sendRedirect("/reg_have_user.html");
//        }else {
//            try(FileOutputStream fos = new FileOutputStream(file);
//                ObjectOutputStream oos = new ObjectOutputStream(fos)) {
//                oos.writeObject(user);
//                response.sendRedirect("/reg_success.html");
//                return;
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
    }


    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameters("username");
        String password = request.getParameters("password");
        if(username==null || password == null  ||username.isEmpty() || password.isEmpty()){
            response.sendRedirect("/login_info_error.html");
            return;
        }

        try (Connection connection = DBUtil.getConnection();){
            String sql1 = "SELECT username,password FROM userinfo WHERE username=? AND password=?;";
            PreparedStatement ps1 = connection.prepareStatement(sql1);
            ps1.setString(1,username);
            ps1.setString(2,password);
            ResultSet rs = ps1.executeQuery();
            if(rs.next()){
                response.sendRedirect("login_success.html");
            }else {
                response.sendRedirect("login_fail.html");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

//
//        File file = new File(userDir,username+".obj");
//        if(file.exists()){
//                try (FileInputStream fis = new FileInputStream(file);
//                     ObjectInputStream ois = new ObjectInputStream(fis)){
//                try {
//                    User user = (User)ois.readObject();
//                    if(user.getPassword().equals(password)){
//                        response.sendRedirect("login_success.html");
//                    }else {
//                        response.sendRedirect("login_fail.html");
//                    }
//                } catch (ClassNotFoundException e) {
//                    e.printStackTrace();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//
//
//        }else {
//            response.sendRedirect("/login_info_error.html");
//        }


    }

}
